﻿using PLCS.Application.Contracts.RoleApp.Dtos;
using PLCS.Application.Contracts.UserApp.Dtos;
using PLCS.Client.IService.Attributes;
using PLCS.Client.IService.UserAuthorization;

namespace PLCS.Client.Service.UserAuthorization;

public class UserService : BaseService<UserDto, UserEditDto>, IUserService
{
    public override async Task DeleteByIdAsync(Guid Id)
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"{typeof(UserDto).Name.ToLower().Replace("dto", "")}?Id={Id}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task ChangePasswordAsync(UserChangePassWordDto user)
    {
        RequestParam requestParam = new RequestParam(Method.Post, "user/changePassword", user);
        var res = await restClient.ExecuteAsync(requestParam);
        if (!res.Status)
            throw new Exception(res.Message);
    }

    public async Task<UserDto> LoginAsync(UserLoginDto user)
    {
        RequestParam requestParam = new RequestParam(Method.Post, "user/login", user);
        var response = await restClient.ExecuteAsync<UserDto>(requestParam);

        if (response.Status)
            return response.Data;
        else
            throw new Exception(response.Message);
    }

    public async Task ResgiterAsync(UserRegisterDto user)
    {
        RequestParam requestParam = new RequestParam(Method.Post, "user/register", user);
        var response = await restClient.ExecuteAsync(requestParam);
        if (!response.Status)
            throw new Exception(response.Message);
    }
}